home *** CD-ROM | disk | FTP | other *** search
/ Champak 125 / Vol 125 (Damaged).iso / games / rabbit_r.swf / scripts / __Packages / smashing / sound / SoundEngine.as
Encoding:
Text File  |  2009-06-09  |  5.9 KB  |  231 lines

  1. class smashing.sound.SoundEngine
  2. {
  3.    var __mc;
  4.    var o_sounds;
  5.    var o_groups;
  6.    var __a_soundQueue;
  7.    var __flag_muted;
  8.    var __flag_overwrite;
  9.    var __groupCount;
  10.    var __callbackPath;
  11.    var __callbackFunc;
  12.    var myName;
  13.    var owner;
  14.    var onSoundComplete;
  15.    var cbpath;
  16.    var __DEFAULTMCNAME = "soundEngine_MC";
  17.    var __DEFAULTGROUPNAME = "sound";
  18.    function SoundEngine()
  19.    {
  20.    }
  21.    function generateSounds(t_path, t_depth, t_overwrite)
  22.    {
  23.       this.__mc = t_path.createEmptyMovieClip(this.__DEFAULTMCNAME,t_depth);
  24.       this.o_sounds = {};
  25.       this.o_groups = {};
  26.       this.__a_soundQueue = [];
  27.       this.__flag_muted = false;
  28.       if(t_overwrite == undefined)
  29.       {
  30.          t_overwrite = false;
  31.       }
  32.       this.__flag_overwrite = t_overwrite;
  33.       this.__groupCount = 0;
  34.       this.createGroup(this.__DEFAULTGROUPNAME);
  35.       trace("-- Init Sound Engine -- ");
  36.    }
  37.    function setCallback(path, func)
  38.    {
  39.       this.__callbackPath = path;
  40.       this.__callbackFunc = func;
  41.    }
  42.    function createGroup(t_name)
  43.    {
  44.       this.__groupCount = this.__groupCount + 1;
  45.       var _loc2_ = this.__mc.createEmptyMovieClip(t_name,this.__groupCount);
  46.       _loc2_.soundObject = new Sound(_loc2_);
  47.       this.o_groups[t_name] = _loc2_;
  48.    }
  49.    function createSound(t_name, t_assetID, t_groupName, doCallback)
  50.    {
  51.       var _loc4_ = undefined;
  52.       if(t_groupName == undefined || t_groupName == "" || t_groupName == null)
  53.       {
  54.          _loc4_ = this.o_groups[this.__DEFAULTGROUPNAME];
  55.       }
  56.       else
  57.       {
  58.          _loc4_ = this.o_groups[t_groupName];
  59.       }
  60.       if(_loc4_ != undefined)
  61.       {
  62.          var _loc2_ = {};
  63.          _loc2_.soundEffect = new Sound(_loc4_);
  64.          _loc2_.soundEffect.attachSound(t_assetID);
  65.          if(doCallback == undefined)
  66.          {
  67.             doCallback = false;
  68.          }
  69.          _loc2_.doCallback = doCallback;
  70.          this.o_sounds[t_name] = _loc2_;
  71.       }
  72.       else
  73.       {
  74.          trace("Error Locating Group " + t_groupName + " for create Sound");
  75.       }
  76.    }
  77.    function playSound(t_soundName, t_loops, t_queued)
  78.    {
  79.       if(this.__flag_muted)
  80.       {
  81.          return undefined;
  82.       }
  83.       var _loc2_ = this.o_sounds[t_soundName];
  84.       if(_loc2_ != undefined)
  85.       {
  86.          if(t_loops == undefined)
  87.          {
  88.             t_loops = 1;
  89.          }
  90.          else if(t_loops == 0)
  91.          {
  92.             t_loops = 100000;
  93.          }
  94.          if(this.__flag_overwrite)
  95.          {
  96.             _loc2_.soundEffect.stop();
  97.          }
  98.          if(t_queued == true)
  99.          {
  100.             this.__a_soundQueue[0].doCallback = _loc2_.doCallback;
  101.             _loc2_.soundEffect.onSoundComplete = mx.utils.Delegate.create(this,this.onQueuedSoundComplete);
  102.          }
  103.          else if(_loc2_.doCallback)
  104.          {
  105.             _loc2_.soundEffect.owner = this;
  106.             _loc2_.soundEffect.myName = t_soundName;
  107.             _loc2_.soundEffect.onSoundComplete = function()
  108.             {
  109.                this.owner.onCallbackSoundComplete(this.myName);
  110.             };
  111.          }
  112.          _loc2_.soundEffect.start(0,t_loops);
  113.       }
  114.       else
  115.       {
  116.          trace("Error - sound " + t_soundName + " not found");
  117.       }
  118.    }
  119.    function stopSound(t_soundName)
  120.    {
  121.       this.o_sounds[t_soundName].soundEffect.stop();
  122.    }
  123.    function stopAll()
  124.    {
  125.       this.clearQueue();
  126.       stopAllSounds();
  127.    }
  128.    function changeVolume(t_vol, t_groupName)
  129.    {
  130.       if(t_groupName == undefined)
  131.       {
  132.          t_groupName = this.__DEFAULTGROUPNAME;
  133.       }
  134.       this.o_groups[t_groupName].soundObject.setVolume(t_vol);
  135.    }
  136.    function toggleSound()
  137.    {
  138.       if(this.__flag_muted)
  139.       {
  140.          this.soundOn();
  141.       }
  142.       else
  143.       {
  144.          this.soundOff();
  145.       }
  146.       return !this.__flag_muted;
  147.    }
  148.    function soundOff()
  149.    {
  150.       this.__flag_muted = true;
  151.    }
  152.    function soundOn()
  153.    {
  154.       this.__flag_muted = false;
  155.    }
  156.    function callbackSound(t_soundName, t_loops, path, func)
  157.    {
  158.       this.playSound(t_soundName,t_loops,false);
  159.       var _loc2_ = this.o_sounds[t_soundName];
  160.       _loc2_.soundEffect.cbpath = path;
  161.       _loc2_.soundEffect.cbfunc = func;
  162.       _loc2_.soundEffect.onSoundComplete = function()
  163.       {
  164.          this.onSoundComplete = null;
  165.          this.cbpath[func]();
  166.       };
  167.    }
  168.    function onCallbackSoundComplete(name)
  169.    {
  170.       this.__callbackPath[this.__callbackFunc](name);
  171.    }
  172.    function queueSound(t_soundName, t_isMusic)
  173.    {
  174.       if(t_isMusic == undefined)
  175.       {
  176.          t_isMusic = false;
  177.       }
  178.       this.__a_soundQueue.push({soundName:t_soundName,isMusic:t_isMusic,isPlaying:false,doCallback:false});
  179.       this.__playQueue();
  180.    }
  181.    function __playQueue()
  182.    {
  183.       if(this.__a_soundQueue.length == 0)
  184.       {
  185.          return undefined;
  186.       }
  187.       if(!this.__a_soundQueue[0].isPlaying)
  188.       {
  189.          if(this.__a_soundQueue[0].isMusic == true)
  190.          {
  191.             this.playMusic(this.__a_soundQueue[0].soundName);
  192.             this.clearQueue();
  193.          }
  194.          else
  195.          {
  196.             this.playSound(this.__a_soundQueue[0].soundName,1,true);
  197.             this.__a_soundQueue[0].isPlaying = true;
  198.          }
  199.       }
  200.    }
  201.    function onQueuedSoundComplete()
  202.    {
  203.       if(this.__a_soundQueue[0].doCallback)
  204.       {
  205.          this.onCallbackSoundComplete(this.__a_soundQueue[0].soundName);
  206.       }
  207.       this.__a_soundQueue.splice(0,1);
  208.       trace("Finish sound");
  209.       this.__playQueue();
  210.    }
  211.    function clearQueue()
  212.    {
  213.       this.__a_soundQueue = [this.__a_soundQueue[0]];
  214.    }
  215.    function get queueLength()
  216.    {
  217.       return this.__a_soundQueue.length;
  218.    }
  219.    function playMusic(name)
  220.    {
  221.    }
  222.    function get isQueuePlaying()
  223.    {
  224.       if(this.__a_soundQueue.length == 0)
  225.       {
  226.          return false;
  227.       }
  228.       return true;
  229.    }
  230. }
  231.